<?php
if (!defined('BASEPATH'))
	exit('No direct script access allowed');

class Payment extends MX_Controller {

	public function __construct() {
		parent::__construct();
		$this->template->set_partial('header_search', 'header/header_search');
		$this->template->set_partial('nav_bar', 'header/nav_bar');

		$this->load->library('Paypal');
	}

	function paypal($amt=0,$proID=0)

	{
		//die($amt);

		$this->paypal->initialize();

		$this->paypal->add_field( 'return', site_url( 'member/payment/success/'.$proID ) );
		$this->paypal->add_field( 'cancel_return', site_url( 'member/payment/cancel' ) );
		//$this->paypal->add_field( 'notify_url', site_url('member/payment/ipn'));

		$this->paypal->add_field( 'item_name', 'Premium Listing');
		$this->paypal->add_field( 'amount', $amt );
		$this->paypal->add_field( 'quantity', '1');

		$this->paypal->paypal_auto_form();

		//$rr = $this->paypal->validate_ipn();
		//print_r($rr);

	}

	/*public function ipn() {

	if ( $this->paypal->validate_ipn() ) {
	$pdata = $this->paypal->ipn_data;
	if ($pdata['txn_type'] == "web_accept") {
	if($pdata['payment_status'] == "Completed"){
	if($pdata['business'] == $this->config->item( 'paypal_email' )) {
	//handle payment...
	}
	}
	}
	}
	}
	*/

	public function success($proID=0) {

		
		if ( $this->paypal->validate_ipn() ) {
			//$pdata = $this->paypal->ipn_data;

			//var_dump($pdata);
			//die();
			
			$pdata = array(
					'payer_email'      => $this->paypal->ipn_data['payer_email'],
					'first_name'       => $this->paypal->ipn_data['first_name'],
					'last_name'        => $this->paypal->ipn_data['last_name'],
					'mc_currency'      => $this->paypal->ipn_data['mc_currency'],
					'payment_gross'    => $this->paypal->ipn_data['payment_gross'],
					'payment_date'     => $this->paypal->ipn_data['payment_date'],
			);
			
			$this->db->insert('paypal_payment',$pdata);
		}
		
		$sql = "update profile set featured = 1 where id=$proID";
		$this->db->query($sql);
		//print $sql;


		$this->load->model('member/profile_model');
		$this->load->model('member/member_model');
		
		if($this->member_model->getMember($this->session->userdata['mem_id'])){
			$data['member']['info'] = $this->member_model->getMember($this->session->userdata['mem_id']);
		}

		$isProfile  = $this->profile_model->getProfileByMemberID($this->session->userdata['mem_id']);
		if($isProfile){
			//	$profile_id = $this->profile_model->getProfileByMemberID($this->session->userdata['mem_id'])->id;
			$data['profile']['info'] = $isProfile;



			//$data['profile_id'] = $profile_id;
			//$data['article']['details'] = $this->article_model->getArticle($profile_id);



		}else{
			$data['profile']['info'] = null;
		}

		$this->template->set_layout('full_layout');
		$this->template->build('success',$data);
		//redirect('member/profile/edit')

	}

	public function cancel() {
		echo "canceled / failed";
	}

}

